package com.pinyougou.sellergoods.service.impl;
import java.util.List;
import java.util.Map;

import com.pinyougou.mapper.TbSpecificationOptionMapper;
import com.pinyougou.pojo.TbSpecificationOption;
import com.pinyougou.pojo.TbSpecificationOptionExample;
import com.pinyougou.pojogroup.SpecificationAndOption;
import org.springframework.beans.factory.annotation.Autowired;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.PageHelper;
import com.pinyougou.mapper.TbSpecificationMapper;
import com.pinyougou.pojo.TbSpecification;
import com.pinyougou.pojo.TbSpecificationExample;
import com.pinyougou.pojo.TbSpecificationExample.Criteria;
import com.pinyougou.sellergoods.service.SpecificationService;


/**
 * 服务实现层
 * @author Administrator
 *
 */
@Service
public class SpecificationServiceImpl implements SpecificationService {

	@Autowired
	private TbSpecificationMapper specificationMapper;

	@Autowired
	private TbSpecificationOptionMapper specificationOptionMapper;
	
	/**
	 * 查询全部
	 */
	@Override
	public List<TbSpecification> findAll() {
		return specificationMapper.selectByExample(null);
	}

	/**
	 * 按分页查询
	 */
//	@Override
//	public PageInfo<TbSpecification> findPage(int pageNum, int pageSize) {
//		PageHelper.startPage(pageNum, pageSize);
//		List<TbSpecification> list = specificationMapper.selectByExample(null);
//		PageInfo<TbSpecification> info = new PageInfo<TbSpecification>(list);
//		return info;
//	}

	/**
	 * 增加
	 */
	@Override
	public void add(SpecificationAndOption specificationAndOption) {

		//获取规格实体
		TbSpecification tbSpecification = specificationAndOption.getSpecification();
        specificationMapper.insert(tbSpecification);

		//获取规格选项集合
		List<TbSpecificationOption> specificationOptionList = specificationAndOption.getSpecificationOptionList();

		for (TbSpecificationOption option : specificationOptionList) {
			option.setSpecId(tbSpecification.getId());//设置规格id
			specificationOptionMapper.insert(option);//插入规格选项
		}
	}

	
	/**
	 * 修改
	 */
	@Override
	public void update(SpecificationAndOption specificationAndOption){
		//保存修改的规格
		TbSpecification tbSpecification = specificationAndOption.getSpecification();
		specificationMapper.updateByPrimaryKey(tbSpecification);

		//先删除原有规格对应的规格选项(应为删除也是修改,所有先清空表格再保存,防止重复)
		TbSpecificationOptionExample optionExample = new TbSpecificationOptionExample();
		TbSpecificationOptionExample.Criteria criteria = optionExample.createCriteria();
		criteria.andSpecIdEqualTo(tbSpecification.getId());
		specificationOptionMapper.deleteByExample(optionExample);

		//获取集合选项
		List<TbSpecificationOption> optionList = specificationAndOption.getSpecificationOptionList();
		for (TbSpecificationOption option : optionList) {
			option.setSpecId(tbSpecification.getId());//设置规格id
			specificationOptionMapper.insert(option);//新增规格
		}

	}	
	
	/**
	 * 根据ID获取实体
	 * @param id
	 * @return
	 */
	@Override
	public SpecificationAndOption findOne(Long id){

		SpecificationAndOption specificationAndOption = new SpecificationAndOption();
		//获取规格实体
		TbSpecification tbSpecification = specificationMapper.selectByPrimaryKey(id);
		specificationAndOption.setSpecification(tbSpecification);

		//获取规格选项详情集合
		TbSpecificationOptionExample example = new TbSpecificationOptionExample();
		TbSpecificationOptionExample.Criteria criteria = example.createCriteria();
		criteria.andSpecIdEqualTo(id);

		List<TbSpecificationOption> options = specificationOptionMapper.selectByExample(example);
		specificationAndOption.setSpecificationOptionList(options);
		return specificationAndOption;

	}

	/**
	 * 批量删除
	 */
	@Override
	public void delete(Long[] ids) {
		for(Long id:ids){
			//删除规格
			specificationMapper.deleteByPrimaryKey(id);
			//删除规格对应的规格选项
			TbSpecificationOptionExample optionExample = new TbSpecificationOptionExample();
			TbSpecificationOptionExample.Criteria criteria = optionExample.createCriteria();
			criteria.andSpecIdEqualTo(id);
			specificationOptionMapper.deleteByExample(optionExample);
		}		
	}

	/**
	 * 搜索
	 * @param specification
	 * @param pageNum 当前页 码
	 * @param pageSize 每页记录数
	 * @return
	 */
	@Override
	public PageInfo<TbSpecification> findPage(TbSpecification specification, int pageNum, int pageSize) {
		PageHelper.startPage(pageNum, pageSize);
		
		TbSpecificationExample example=new TbSpecificationExample();
		Criteria criteria = example.createCriteria();
		
		if(specification!=null){			
						if(specification.getSpecName()!=null && specification.getSpecName().length()>0){
				criteria.andSpecNameLike("%"+specification.getSpecName()+"%");
			}
	
		}

		List<TbSpecification> list = specificationMapper.selectByExample(example);
		PageInfo<TbSpecification> info = new PageInfo<TbSpecification>(list);
		return info;
	}

	@Override
	public List<Map> selectOptionList() {
		return specificationMapper.selectOptionList();
	}

}
